<template>
  <div class="header-wrap">
    <div class="header-container">
      <div v-if="userInfo" class="btn-wrap">
        <span @click="toUserInfo">{{userInfo.name}}</span>
        <span>余额：<i>{{userInfo.balance}}</i>元</span>
        <span class="btn" @click="toRecharge">充值</span>
        <span class="btn" @click="loginout">退出</span>
      </div>
      <div class="btn-wrap" v-else>
        <span class="btn" @click="login">登录</span>
        <span class="btn" @click="regist">注册</span>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex';

export default {
  data() {
    return {
      name: 'Header'
    };
  },
  computed: {
    ...mapState(['userInfo', 'eventBus'])
  },
  methods: {
    login() {
      this.$store.commit('SHOW_LOGIN', true);
    },
    regist() {
      this.$store.commit('SHOW_REGIST');
    },
    loginout() {
      this.$store.commit('LOGOUT');
      this.login();
    },
    toUserInfo() {
      if (this.$route.name !== 'UserCenter') {
        this.$router.push({
          name: 'UserCenter'
        });
      }
    },
    toRecharge() {
      if (this.$route.name !== 'UserCenter') {
        this.$router.push({
          name: 'UserCenter',
          params: {
            type: 'recharge'
          }
        });
      } else {
        this.eventBus.$emit('recharge', '充值');
      }
    }
  }
};
</script>

<style lang="less" scoped>
  @import '../less/global.less';
  .header-wrap {
    height: 40px;
    line-height: 40px;
    background-color: #00a895;
    font-size: 12px;
    color: #fff;
    .header-container {
      width: @mainWidth;
      height: 100%;
      margin: 0 auto;
      .btn-wrap {
        float: right;
        span {
          padding: 0 10px;
          border-right: 1px solid #ddd;
          &:last-child {
            border: 0;
          }
          i {
            font-style: normal;
          }
        }
        .btn {
          cursor: pointer;
          &:hover {
            color: #fcff00;
          }
        }
      }
      .username {
        margin-right: 10px;
        &:hover {
          color: #fcff00;
          cursor: pointer;
        }
      }
    }
  }
</style>
